package pers.qianyu.month_202101.date_20210125;

import pers.qianyu.util.model.*;

/**
 * 29. 删除链表中重复的节点
 * https://www.acwing.com/problem/content/27/
 *
 * @author mizzle rain
 * @date 2021-01-25 14:19
 */
public class DeleteDuplication {
    public ListNode deleteDuplication(ListNode head) {
        if (head == null) return null;
        ListNode root = new ListNode(-1), p = root;
        for (ListNode cur = head, pre; cur != null; ) {
            pre = cur;
            while (cur != null && cur.val == pre.val) {
                cur = cur.next;
            }
            if (pre.next == cur) {
                p.next = pre;
                p = p.next;
                p.next = null;
            }
        }
        return root.next;
    }
}
